package com.cspm.ahjy.bc.office.word.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Element;
import org.springframework.util.StringUtils;

import com.cspm.ahjy.bc.office.util.StringUtil;
import com.cspm.ahjy.bc.office.util.UnitConvertUtil;
import com.cspm.ahjy.bc.office.word.domain.MSAction;
import com.cspm.ahjy.bc.office.word.domain.MSActionType;
import com.cspm.ahjy.bc.office.word.domain.MSFont;
import com.cspm.ahjy.bc.office.word.domain.MSWordart;
import com.cspm.ahjy.bc.office.word.domain.MSWordartStyle;

/**
 * 艺术字操作及匹配
 * 
 * @author ServerZhang
 * @date 2017年9月28日
 */
public class WordartUtil {

	/**
	 * 添加艺术字
	 * 
	 * @param font2
	 * @return
	 * @author ServerZhang
	 * @date 2017年9月28日
	 */
	public static List<MSAction> addWordart(MSWordart wordart,
			Integer paragraphNo, Boolean wholeParagraph) {
		// TODO 添加艺术字，暂无此需求
		return getWordartStyle(wordart, paragraphNo, wholeParagraph);
	}

	/**
	 * 段落中的文字修改为艺术字
	 * 
	 * @param font1
	 * @param font2
	 * @param paragraphNo
	 *            比较的段落号
	 * @param wholeParagraph
	 *            是否是整段文字的比较，默认为false
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月11日
	 */
	public static List<MSAction> compare(MSFont font1, MSWordart font2,
			Integer paragraphNo, Boolean wholeParagraph) {
		List<MSAction> actions = new ArrayList<MSAction>();

		return getWordartStyle(font2, paragraphNo, wholeParagraph);

	}

	/**
	 * 艺术字差异比较
	 * 
	 * @param font1
	 * @param font2
	 * @return
	 * @author ServerZhang
	 * @date 2017年9月29日
	 */
	public static  List<MSAction> compare(MSWordart wordart1, MSWordart wordart2,
			Integer paragraphNo, Boolean wholeParagraph) {
		List<MSAction> actions = new ArrayList<MSAction>();
		
		if (wholeParagraph == null) {
			wholeParagraph = Boolean.FALSE;
		}

		Integer[] paragraphNos = new Integer[1];
		if (paragraphNo != null) {
			paragraphNos[0] = paragraphNo;
		}

		String text1 = wordart1.getText();
		String text2 = wordart2.getText();
		
		if (text2 == null || text1 == null || !text1.equals(text2)) {
			return actions;
		}
		
		String style1 = wordart1.getStyle();
		String style2 = wordart2.getStyle();
		if (!StringUtils.isEmpty(style2) && !style2.equals(style1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_wordart_style");
				action.setText("");
			} else {
				actionType.setType("font_wordart_style");
				action.setText(text2);
			}
			actionType.setTypeValue(style2);
			action.setParagraphNos(paragraphNos);
			String content = "修改为艺术字，样式为 " + style2;
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		String fontName1 = wordart1.getFontName();
		String fontName2 = wordart2.getFontName();
		if (!StringUtils.isEmpty(fontName2) && !fontName2.equals(fontName1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_wordart_fontName");
				action.setText("");
			} else {
				actionType.setType("font_wordart_fontName");
				action.setText(text2);
			}
			actionType.setTypeValue(fontName2);
			action.setParagraphNos(paragraphNos);
			String content = "修改为艺术字，字体为 " + fontName2;
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		Integer fontSize1 = wordart1.getFontSize();
		Integer fontSize2 = wordart2.getFontSize();
		if (fontSize2 != null && !fontSize2.equals(0) && !fontSize2.equals(fontSize1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_wordart_fontSize");
				action.setText("");
			} else {
				actionType.setType("font_wordart_fontSize");
				action.setText(text2);
			}
			actionType.setTypeValue(fontSize2);
			action.setParagraphNos(paragraphNos);
			String content = "修改为艺术字，字号为 "
					+ UnitConvertUtil.standar2PT(fontSize2);
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		Boolean bold1 = wordart1.getBold();
		Boolean bold2 = wordart2.getBold();
		
		if (bold2) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_wordart_bold");
				action.setText("");
			} else {
				actionType.setType("font_wordart_bold");
				action.setText(text2);
			}
			actionType.setTypeValue(bold2);
			action.setParagraphNos(paragraphNos);
			String content = "修改为艺术字，加粗 ";
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		Boolean italic1 = wordart1.getItalic();
		Boolean italic2 = wordart2.getItalic();
		if (italic2) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_wordart_italic");
				action.setText("");
			} else {
				actionType.setType("font_wordart_italic");
				action.setText(text2);
			}
			actionType.setTypeValue(italic2);
			action.setParagraphNos(paragraphNos);
			String content = "修改为艺术字，倾斜";
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}

		return actions;
	}

	/**
	 * 从<v:pict>标签下获取艺术字
	 * @param pictElement
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月18日
	 */
	public static MSWordart getWordart4Pict(Element pictElement) {
		MSWordart wordart = new MSWordart();

		Element shapeElement = pictElement.element("shape");//
		if (null == shapeElement) {
			return wordart;
		}

		MSWordartStyle msWordartStyle = WordartStyleUtil.getMsWordartStyle(shapeElement);
		if (msWordartStyle != null && msWordartStyle.getStyleNo() != null) {
			wordart.setMsWordartStyle(msWordartStyle);
			String location = msWordartStyle.getLocation();
			if (location != null) {
				wordart.setStyle(location);
System.out.println(location);
			}
		}
		
		Element tp = shapeElement.element("textpath");
		if (tp != null) {
			Attribute string = tp.attribute("string");
			if (string != null) {
				wordart.setText(string.getText());
			}
			Attribute style = tp.attribute("style");
			if (style != null) {
				String text = style.getText();
				Map<String, String> map = StringUtil.toMap(text);
				String font = map.get("font-family");
				if (!StringUtils.isEmpty(font)) {
					wordart.setFontName(font.replaceAll("\"", ""));
				}
				String size = map.get("font-size");
				if (!StringUtils.isEmpty(size)) {
					wordart.setFontSize(UnitConvertUtil.convert2Standard(size));
					// wordart.setFontSize(Integer.parseInt(size.replaceAll("pt",
					// "")));
				}
				String weight = map.get("font-weight");
				if (!StringUtils.isEmpty(weight)) {
					wordart.setBold(true);
				}
				String italic = map.get("font-style");
				if (!StringUtils.isEmpty(italic)) {
					wordart.setItalic(true);
				}
			}
		}
		Attribute fillcolor = shapeElement.attribute("fillcolor");
		if (fillcolor != null) {
			wordart.setColor(fillcolor.getText());
		}
		return wordart;
	}

	/**
	 * 获取艺术字样式
	 * 
	 * @param wordart
	 * @param paragraphNo
	 * @param wholeParagraph
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月11日
	 */
	private static List<MSAction> getWordartStyle(MSWordart wordart,
			Integer paragraphNo, Boolean wholeParagraph) {
		List<MSAction> actions = new ArrayList<MSAction>();

		if (wholeParagraph == null) {
			wholeParagraph = Boolean.FALSE;
		}

		Integer[] paragraphNos = new Integer[1];
		if (paragraphNo != null) {
			paragraphNos[0] = paragraphNo;
		}

		String text = wordart.getText();
		String fontName = wordart.getFontName();
		Integer fontSize = wordart.getFontSize();
		Boolean bold = wordart.getBold();
		Boolean italic = wordart.getItalic();
		String style = wordart.getStyle();
		if (!StringUtils.isEmpty(style)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_wordart_style");
				action.setText("");
			} else {
				actionType.setType("font_wordart_style");
				action.setText(text);
			}
			actionType.setTypeValue(style);
			action.setParagraphNos(paragraphNos);
			String content = "修改为艺术字，样式为 " + style;
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (!StringUtils.isEmpty(fontName)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_wordart_fontName");
				action.setText("");
			} else {
				actionType.setType("font_wordart_fontName");
				action.setText(text);
			}
			actionType.setTypeValue(fontName);
			action.setParagraphNos(paragraphNos);
			String content = "修改为艺术字，字体为 " + fontName;
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (fontSize != null && !fontSize.equals(0)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_wordart_fontSize");
				action.setText("");
			} else {
				actionType.setType("font_wordart_fontSize");
				action.setText(text);
			}
			actionType.setTypeValue(fontSize);
			action.setParagraphNos(paragraphNos);
			String content = "修改为艺术字，字号为 "
					+ UnitConvertUtil.standar2PT(fontSize);
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (bold) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_wordart_bold");
				action.setText("");
			} else {
				actionType.setType("font_wordart_bold");
				action.setText(text);
			}
			actionType.setTypeValue(bold);
			action.setParagraphNos(paragraphNos);
			String content = "修改为艺术字，加粗 ";
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		if (italic) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			if (wholeParagraph) {
				actionType.setType("paragraph_font_wordart_italic");
				action.setText("");
			} else {
				actionType.setType("font_wordart_italic");
				action.setText(text);
			}
			actionType.setTypeValue(italic);
			action.setParagraphNos(paragraphNos);
			String content = "修改为艺术字，倾斜";
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}

		return actions;
	}

}
